﻿#if DebugTest
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Ivan.Dm.ChromeRemoteDebugger.Transport;
using System.Net.Sockets;
using System.Net;
using System.Threading;

namespace Ivan.Dm.ChromeRemoteDebugger.Tests {
#warning TODO finish it
    public class BrowserStub : IDisposable {
        public readonly IPEndPoint EndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9222);
        private ManualResetEvent TerminateEvent { get; set; }
        private Socket Socket { get; set; }
        List<Message> Responses { get; set; }

        public BrowserStub() {
            Socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            Socket.Bind(EndPoint);
            Socket.Listen(1);
            TerminateEvent = new ManualResetEvent(false);
            var listenerThread = new Thread(this.ListenerThread);
            listenerThread.Start();
        }

        public BrowserStub(List<Message> responses)
            : this() {
            Responses = responses;
        }

        private void ListenerThread() {
            Socket acceptSocket = null;
            while (true) {
                if (TerminateEvent.WaitOne(0))
                    return;
                acceptSocket = Socket.Accept();
            }
        }

        void IDisposable.Dispose() {
            TerminateEvent.Set();
        }
    }
}
#endif